﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:DlyStorage
    /// </summary>
    public partial class DlyStorage
    {
        public DlyStorage()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("DlyOrder", "DlyStorage");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int DlyOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from DlyStorage");
            strSql.Append(" where DlyOrder=@DlyOrder");
            var parameters = new SqlParameter[] { new SqlParameter("@DlyOrder", SqlDbType.Int, 4)
            };
            parameters[0].Value = DlyOrder;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.DlyStorage model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into DlyStorage(");
            strSql.Append("Vchcode,ktypeid,ptypeid,Qty,price,total,date,OrderType,OrderCode,OrderDlyCode,DeptID,PStutas,unit,Comment,unit1,qty1)");
            strSql.Append(" values (");
            strSql.Append("@Vchcode,@ktypeid,@ptypeid,@Qty,@price,@total,@date,@OrderType,@OrderCode,@OrderDlyCode,@DeptID,@PStutas,@unit,@Comment,@unit1,@qty1)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.Decimal, 5),
                    new SqlParameter("@ktypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@ptypeid", SqlDbType.VarChar, 100),
                    new SqlParameter("@Qty", SqlDbType.Decimal, 9),
                    new SqlParameter("@price", SqlDbType.Decimal, 9),
                    new SqlParameter("@total", SqlDbType.Decimal, 9),
                    new SqlParameter("@date", SqlDbType.DateTime),
                    new SqlParameter("@OrderType", SqlDbType.Int, 4),
                    new SqlParameter("@OrderCode", SqlDbType.Int, 4),
                    new SqlParameter("@OrderDlyCode", SqlDbType.Int, 4),
                    new SqlParameter("@DeptID", SqlDbType.VarChar, 25),
                    new SqlParameter("@PStutas", SqlDbType.Int, 4),
                    new SqlParameter("@unit", SqlDbType.Int, 4),
                    new SqlParameter("@Comment", SqlDbType.VarChar, 256),
                    new SqlParameter("@unit1", SqlDbType.Int, 4),
                    new SqlParameter("@qty1", SqlDbType.Decimal, 9) };
            parameters[0].Value = model.Vchcode;
            parameters[1].Value = model.ktypeid;
            parameters[2].Value = model.ptypeid;
            parameters[3].Value = model.Qty;
            parameters[4].Value = model.price;
            parameters[5].Value = model.total;
            parameters[6].Value = model.date;
            parameters[7].Value = model.OrderType;
            parameters[8].Value = model.OrderCode;
            parameters[9].Value = model.OrderDlyCode;
            parameters[10].Value = model.DeptID;
            parameters[11].Value = model.PStutas;
            parameters[12].Value = model.unit;
            parameters[13].Value = model.Comment;
            parameters[14].Value = model.unit1;
            parameters[15].Value = model.qty1;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.DlyStorage model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update DlyStorage set ");
            strSql.Append("Vchcode=@Vchcode,");
            strSql.Append("ktypeid=@ktypeid,");
            strSql.Append("ptypeid=@ptypeid,");
            strSql.Append("Qty=@Qty,");
            strSql.Append("price=@price,");
            strSql.Append("total=@total,");
            strSql.Append("date=@date,");
            strSql.Append("OrderType=@OrderType,");
            strSql.Append("OrderCode=@OrderCode,");
            strSql.Append("OrderDlyCode=@OrderDlyCode,");
            strSql.Append("DeptID=@DeptID,");
            strSql.Append("PStutas=@PStutas,");
            strSql.Append("unit=@unit,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("unit1=@unit1,");
            strSql.Append("qty1=@qty1");
            strSql.Append(" where DlyOrder=@DlyOrder");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.Decimal, 5),
                    new SqlParameter("@ktypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@ptypeid", SqlDbType.VarChar, 100),
                    new SqlParameter("@Qty", SqlDbType.Decimal, 9),
                    new SqlParameter("@price", SqlDbType.Decimal, 9),
                    new SqlParameter("@total", SqlDbType.Decimal, 9),
                    new SqlParameter("@date", SqlDbType.DateTime),
                    new SqlParameter("@OrderType", SqlDbType.Int, 4),
                    new SqlParameter("@OrderCode", SqlDbType.Int, 4),
                    new SqlParameter("@OrderDlyCode", SqlDbType.Int, 4),
                    new SqlParameter("@DeptID", SqlDbType.VarChar, 25),
                    new SqlParameter("@PStutas", SqlDbType.Int, 4),
                    new SqlParameter("@unit", SqlDbType.Int, 4),
                    new SqlParameter("@Comment", SqlDbType.VarChar, 256),
                    new SqlParameter("@unit1", SqlDbType.Int, 4),
                    new SqlParameter("@qty1", SqlDbType.Decimal, 9),
                    new SqlParameter("@DlyOrder", SqlDbType.Int, 4) };
            parameters[0].Value = model.Vchcode;
            parameters[1].Value = model.ktypeid;
            parameters[2].Value = model.ptypeid;
            parameters[3].Value = model.Qty;
            parameters[4].Value = model.price;
            parameters[5].Value = model.total;
            parameters[6].Value = model.date;
            parameters[7].Value = model.OrderType;
            parameters[8].Value = model.OrderCode;
            parameters[9].Value = model.OrderDlyCode;
            parameters[10].Value = model.DeptID;
            parameters[11].Value = model.PStutas;
            parameters[12].Value = model.unit;
            parameters[13].Value = model.Comment;
            parameters[14].Value = model.unit1;
            parameters[15].Value = model.qty1;
            parameters[16].Value = model.DlyOrder;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int DlyOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DlyStorage ");
            strSql.Append(" where DlyOrder=@DlyOrder");
            var parameters = new SqlParameter[] { new SqlParameter("@DlyOrder", SqlDbType.Int, 4)
            };
            parameters[0].Value = DlyOrder;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string DlyOrderlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DlyStorage ");
            strSql.Append(" where DlyOrder in (" + DlyOrderlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DlyStorage GetModel(int DlyOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 Vchcode,ktypeid,ptypeid,Qty,price,total,date,OrderType,OrderCode,OrderDlyCode,DlyOrder,DeptID,PStutas,unit,Comment,unit1,qty1 from DlyStorage ");
            strSql.Append(" where DlyOrder=@DlyOrder");
            var parameters = new SqlParameter[] { new SqlParameter("@DlyOrder", SqlDbType.Int, 4)
            };
            parameters[0].Value = DlyOrder;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DlyStorage DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.DlyStorage();
            if (row != null)
            {
                if (row["Vchcode"] != null && row["Vchcode"].ToString() != string.Empty)
                {
                    model.Vchcode = decimal.Parse(row["Vchcode"].ToString());
                }
                if (row["ktypeid"] != null)
                {
                    model.ktypeid = row["ktypeid"].ToString();
                }
                if (row["ptypeid"] != null)
                {
                    model.ptypeid = row["ptypeid"].ToString();
                }
                if (row["Qty"] != null && row["Qty"].ToString() != string.Empty)
                {
                    model.Qty = decimal.Parse(row["Qty"].ToString());
                }
                if (row["price"] != null && row["price"].ToString() != string.Empty)
                {
                    model.price = decimal.Parse(row["price"].ToString());
                }
                if (row["total"] != null && row["total"].ToString() != string.Empty)
                {
                    model.total = decimal.Parse(row["total"].ToString());
                }
                if (row["date"] != null && row["date"].ToString() != string.Empty)
                {
                    model.date = DateTime.Parse(row["date"].ToString());
                }
                if (row["OrderType"] != null && row["OrderType"].ToString() != string.Empty)
                {
                    model.OrderType = int.Parse(row["OrderType"].ToString());
                }
                if (row["OrderCode"] != null && row["OrderCode"].ToString() != string.Empty)
                {
                    model.OrderCode = int.Parse(row["OrderCode"].ToString());
                }
                if (row["OrderDlyCode"] != null && row["OrderDlyCode"].ToString() != string.Empty)
                {
                    model.OrderDlyCode = int.Parse(row["OrderDlyCode"].ToString());
                }
                if (row["DlyOrder"] != null && row["DlyOrder"].ToString() != string.Empty)
                {
                    model.DlyOrder = int.Parse(row["DlyOrder"].ToString());
                }
                if (row["DeptID"] != null)
                {
                    model.DeptID = row["DeptID"].ToString();
                }
                if (row["PStutas"] != null && row["PStutas"].ToString() != string.Empty)
                {
                    model.PStutas = int.Parse(row["PStutas"].ToString());
                }
                if (row["unit"] != null && row["unit"].ToString() != string.Empty)
                {
                    model.unit = int.Parse(row["unit"].ToString());
                }
                if (row["Comment"] != null)
                {
                    model.Comment = row["Comment"].ToString();
                }
                if (row["unit1"] != null && row["unit1"].ToString() != string.Empty)
                {
                    model.unit1 = int.Parse(row["unit1"].ToString());
                }
                if (row["qty1"] != null && row["qty1"].ToString() != string.Empty)
                {
                    model.qty1 = decimal.Parse(row["qty1"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Vchcode,ktypeid,ptypeid,Qty,price,total,date,OrderType,OrderCode,OrderDlyCode,DlyOrder,DeptID,PStutas,unit,Comment,unit1,qty1 ");
            strSql.Append(" FROM DlyStorage ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Vchcode,ktypeid,ptypeid,Qty,price,total,date,OrderType,OrderCode,OrderDlyCode,DlyOrder,DeptID,PStutas,unit,Comment,unit1,qty1 ");
            strSql.Append(" FROM DlyStorage ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DlyStorage ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.DlyOrder desc");
            }
            strSql.Append(")AS Row, T.*  from DlyStorage T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

